﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Project362.Files
{
    [Serializable()]
    class MetaHandler: ISerializable
    {
	    private Dictionary<string,string> metadata;
	    public MetaHandler()
	    {
		    metadata = new Dictionary<string,string>();
	    }
        public MetaHandler(SerializationInfo info, StreamingContext stream)
        {
            this.metadata = (Dictionary<string, string>)info.GetValue("Dictionary", typeof(Dictionary<string, string>));
        }
        public List<string> getAllKeys()
        {
            List<string> keys=new List<string>();
            foreach (KeyValuePair<string, string> kvp in metadata)
            {
                keys.Add(kvp.Key);
            }
            return keys;
        }
	    public bool addAttribute(string name, string content)
	    {
            if (!metadata.ContainsKey(name))
            {
                metadata.Add(name, content);
                return true;
            }
            else return false;
	    }
	    public bool removeAttribute(string name)
	    {
            if (metadata.ContainsKey(name))
            {
                metadata.Remove(name);
                return true;
            }
            else return false;
	    }
	    public bool editAttributeContent(string name, string content)
	    {
            if (metadata.ContainsKey(name))
            {
                metadata[name] = content;
                return true;
            }
		    else return false;
	    }
        public string getMetadataContents(string meta)
        {
            foreach (KeyValuePair<string,string> kvp in metadata)
            {
                if (kvp.Key == meta) return kvp.Value;
            }
            return null;
        }
        public List<string> getAll()
        {
            List<string> l = new List<string>();
            foreach (KeyValuePair<string, string> kvp in metadata)
            {
                l.Add(kvp.Key + ": " + kvp.Value);
            }
            return l;
        }
        public void GetObjectData(SerializationInfo info, StreamingContext stream)
        {
            info.AddValue("Dictionary", metadata);
        }
    }
}
